<?php
/**
 * Created by PhpStorm.
 * User: Season
 * Date: 2018/10/24
 * Time: 15:19
 */

namespace app\api\validate;


use app\lib\exception\ParameterExcption;

class OrderPlace extends BaseValidate
{
    protected $rule = [
        'products' => 'checkProducts'
    ];

    protected $singleRule = [
        'product_id' => 'require|isPositiveInteger',
        'count' => 'require|isPositiveInteger',
    ];

    protected function checkProducts($values){
        if(!is_array ($values)){
            throw new ParameterExcption([
                                            'msg' => '商品列表必须是数组'
                                        ]);
        }
        if(empty($values)){
            throw new ParameterExcption([
                                            'msg' => '商品列表不能为空'
                                        ]);
        }
        foreach ($values as $item){
            $this->checkProduct ($item);
        }
        return true;
    }

    protected function  checkProduct($value){
        $validate = new BaseValidate($this->singleRule);
        $result = $validate->check ($value);
        if(!$result){
            throw  new ParameterExcption([
                                             'msg' => '商品列表参数错误'
                                         ]);
        }
    }
}